package com.xds.springcloud.controller;
import com.alibaba.druid.sql.PagerUtils;
import com.xds.springcloud.entity.CommonResult;
import com.xds.springcloud.entity.Payment;
import com.xds.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-09-11
 */
@RestController
@RequestMapping("/payment")
@Slf4j

public class PaymentController {
    @Autowired
    private PaymentService paymentService;
    @Value("${server.port}")
    private String serverPort;
    @Autowired
    private DiscoveryClient  discoveryClient;
    @PostMapping("create")
    public CommonResult create( @RequestBody Payment payment){
        boolean save = paymentService.save(payment);
        if (save){
            return new CommonResult(200, "插入成功,serverPort"+serverPort);
        }else {
            return new CommonResult(201, "插入失败");
        }
    }
    @GetMapping("get/{id}")
    public CommonResult get(@PathVariable("id") Long id ){
        Payment payment = paymentService.getById(id);
        log.info("查询结果"+payment);
        if (payment!=null){
            return new CommonResult(200, "查询成功,serverPort"+serverPort,payment);
        }else {
            return new CommonResult(201, "查询不到,id"+id,null);
        }
    }
    @GetMapping("discovery")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("*****service:"+service);
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
        }
        return this.discoveryClient;
    }
    @GetMapping("lb")
    public String getPaymentLB(){
        return serverPort;
    }
    @GetMapping("zipkin")
    public String paymentZipkin(){
        return "hi, i is guanhui taba";
    }

}

